
#if !defined(AFX_GLUTBIND_H__0A8840CC_3632_4286_B67D_C4767C7269E3__INCLUDED_)
#define AFX_GLUTBIND_H__0A8840CC_3632_4286_B67D_C4767C7269E3__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "SQFunction.h"
#include "../HaruLib/include/hpdf.h"
#pragma comment (lib ,"HaruLib.lib")
#pragma comment (lib ,"libpng.lib")
#pragma comment (lib ,"zlib.lib")
#include "../squirrel/sqfuncproto.h"
#include "../squirrel/sqclosure.h"
SqFunction<void*> Haru_Alloc_Func;
SqFunction<void> Haru_Free_Func;
SqFunction<void> HaruErrofHandler;

void error_handler (HPDF_STATUS   error_no,
					HPDF_STATUS   detail_no,
					void         *user_data)
{
	// 	printf ("ERROR: error_no=%04X, detail_no=%u\n",
	// 		(HPDF_UINT)error_no, (HPDF_UINT)detail_no);
	if (HaruErrofHandler.m_strfunctionName!=NULL)
	{
		HaruErrofHandler(error_no,detail_no,user_data);
	}

	throw std::exception ();
}
void* g_HPDF_Alloc_Func  (HPDF_UINT  size)
{
	if (Haru_Alloc_Func.m_strfunctionName!=NULL)
	{
		return Haru_Alloc_Func(size);
	}else{
		return NULL;
	}

}
void g_HPDF_Free_Func  (void  *aptr)
{
	//HPDF_Free_Func
	if (Haru_Free_Func.m_strfunctionName!=NULL)
	{
		Haru_Free_Func(aptr);
	}
}
/*
//The codes here are some test things.
SQInteger print_args(HSQUIRRELVM v)
{
	SQInteger nargs = sq_gettop(v); //number of arguments

	for(SQInteger n=1;n<=nargs;n++)
	{
		printf("arg %d is ",n);
		switch(sq_gettype(v,n))
		{
		case OT_NULL:
			printf("null");        
			break;
		case OT_INTEGER:
			printf("integer");
			break;
		case OT_FLOAT:
			printf("float");
			break;
		case OT_STRING:
			printf("string");
			break;    
		case OT_TABLE:
			printf("table");
			break;
		case OT_ARRAY:
			printf("array");
			break;
		case OT_USERDATA:
			printf("userdata");
			break;
		case OT_CLOSURE:
			{
				SqTypeWrapper x4(stack_get(v, n));
				printf("closure(function)");    
				printf("Name:%s",x4.sqobj._unVal.pClosure->_function->_name._unVal.pString->_val);
				break;
			}

		case OT_NATIVECLOSURE:
			printf("native closure(C function)");
			break;
		case OT_GENERATOR:
			printf("generator");
			break;
		case OT_USERPOINTER:
			printf("userpointer");
			break;
		case OT_CLASS:
			printf("class");
			break;
		case OT_INSTANCE:
			printf("instance");
			break;
		case OT_WEAKREF:
			printf("weak reference");
			break;
		default:
			return sq_throwerror(v,"invalid param"); //throws an exception
		}
		printf("%s",". ");

	}
	printf("\n");
	sq_pushinteger(v,nargs); //push the number of arguments as return value
	return 1; //1 because 1 value is returned
}

template <>
SqFunction<void> Get(TypeWrapper<SqFunction<void>>,HSQUIRRELVM v,SQInteger index)
{
SqTypeWrapper x4(stack_get(v, index));	
SqFunction<void> func(v,x4.sqobj._unVal.pClosure->_function->_name._unVal.pString->_val); 

return func;
}
int TestSqCallback (SqFunction<void> func,int x)
{
func(x);
return 1;
}
void TestBindX(HSQUIRRELVM v)
{


//The codes here are some test things.
	register_global_func(v,print_args,_SC("PrintArg"));

	RegisterGlobal(v,TestSqCallback,_SC("TestSqCallback"));
}
*/
#define  TESTCODE
#include "SquirrelBindingsUtils.h"
#include "../squirrel/sqclass.h"
#ifdef TESTCODE
typedef struct D3DXVECTOR3 {
	float x;
	float y;
	float z;
} D3DXVECTOR3, *LPD3DXVECTOR3;





_DECL_CLASS(Vector3);

_IMPL_NATIVE_CONSTRUCTION(Vector3,D3DXVECTOR3);

_MEMBER_FUNCTION_IMPL(Vector3,constructor)
{
	D3DXVECTOR3 temp;
	D3DXVECTOR3 *newv = NULL;
	StackHandler sa(v);
	int nparams = sa.GetParamCount();
	switch(nparams) {
	case 1:
		temp.x = 0; 
		temp.y = 0;
		temp.z = 0;
		break;
	case 2:
		if(sa.GetType(2) == OT_INSTANCE) {
			//_CHECK_INST_PARAM(vec,2,D3DXVECTOR3,Vector3);
			//if(vec)	temp = *vec;
			//else return sa.ThrowError(_T("Vector3() invalid instance type"));
		}
		break;
	case 4:
		temp.x = sa.GetFloat(2);
		temp.y = sa.GetFloat(3);
		temp.z = sa.GetFloat(4);
		break;
	default:
		return -1/*sa.ThrowError(_T("Vector3() wrong parameters"))*/;
	}
	newv = new D3DXVECTOR3(temp);
	return construct_Vector3(v,newv);
}
_MEMBER_FUNCTION_IMPL(Vector3,_get)
{
	StackHandler sa(v);
	_CHECK_SELF(D3DXVECTOR3,Vector3);
	const SQChar *s = sa.GetString(2);
	if(s && (s[1] != 0))
		return SQ_ERROR;
	int index = s && (s[1] == 0)?s[0]:sa.GetInt(2);
	switch(index) {
		case 0: case 'x': case 'r': return sa.Return(self->x); break;
		case 1: case 'y': case 'g':	return sa.Return(self->y); break;
		case 2: case 'z': case 'b': return sa.Return(self->z); break;
	}
	return SQ_ERROR;
}


_MEMBER_FUNCTION_IMPL(Vector3,_set)
{
	StackHandler sa(v);
	_CHECK_SELF(D3DXVECTOR3,Vector3);
	const SQChar *s = sa.GetString(2);
	int index = s?s[0]:sa.GetInt(2);
	switch(index) {
	case 0: case 'x': case 'r':
		return sa.Return(self->x = sa.GetFloat(3));
		break;
	case 1: case 'y': case 'g':
		return sa.Return(self->y = sa.GetFloat(3));
		break;
	case 2: case 'z': case 'b':
		return sa.Return(self->z = sa.GetFloat(3));
		break;
	}

	return SQ_ERROR;
}


_BEGIN_CLASS(Vector3)
_MEMBER_FUNCTION(Vector3,constructor,-1,(".n|xnn"))
_MEMBER_FUNCTION(Vector3,_set,3,_T("xs|n"))
_MEMBER_FUNCTION(Vector3,_get,2,_T("xs|n"))
_END_CLASS(Vector3)

SQInteger print_args(HSQUIRRELVM v)
{
	SQInteger nargs = sq_gettop(v); //number of arguments

	for(SQInteger n=1;n<=nargs;n++)
	{
		printf("arg %d is ",n);
		switch(sq_gettype(v,n))
		{
		case OT_NULL:
			printf("null");        
			break;
		case OT_INTEGER:
			printf("integer");
			break;
		case OT_FLOAT:
			printf("float");
			break;
		case OT_STRING:
			printf("string");
			break;    
		case OT_TABLE:
			printf("table");
			break;
		case OT_ARRAY:
			{
				printf("\nType:array\n");
				SqTypeWrapper x4(stack_get(v, n));
				for (int i = 0;i< x4.sqobj._unVal.pArray->_values.size();i++)
				{
					printf("array[%d]:%d@%06X\n",i,x4.sqobj._unVal.pArray->_values[i]._unVal.nInteger,&(x4.sqobj._unVal.pArray->_values[i]._unVal.nInteger));
				}
				printf("sizeof(int):%06X\n",sizeof(SQInteger));
				printf("sizeof(SQObject):%06X\n",sizeof(SQObject));
				printf("sizeof(SQObjectPtr):%06X\n",sizeof(SQObjectPtr));
			}
			
			break;
		case OT_USERDATA:
			printf("userdata");
			break;
		case OT_CLOSURE:
			{
				SqTypeWrapper x4(stack_get(v, n));
				printf("closure(function)");    
				printf("Name:%s",x4.sqobj._unVal.pClosure->_function->_name._unVal.pString->_val);
				break;
			}

		case OT_NATIVECLOSURE:
			printf("native closure(C function)");
			break;
		case OT_GENERATOR:
			printf("generator");
			break;
		case OT_USERPOINTER:
			printf("userpointer");
			break;
		case OT_CLASS:
			printf("class");
			break;
		case OT_INSTANCE:
			{
				printf("instance");
				SqTypeWrapper x4(stack_get(v, n));
				D3DXVECTOR3* pd3dvec = (D3DXVECTOR3*)(x4.sqobj._unVal.pInstance->_userpointer);
				printf("\nx:%.3f,y:%.3f,z:%.3f",pd3dvec->x,pd3dvec->y,pd3dvec->z);
	
			}
			
			break;
		case OT_WEAKREF:
			printf("weak reference");
			break;
		default:
			return sq_throwerror(v,"invalid param"); //throws an exception
		}
		printf("%s",". ");

	}
	printf("\n");
	sq_pushinteger(v,nargs); //push the number of arguments as return value
	return 1; //1 because 1 value is returned
}
#endif // TESTCODE
template <typename RT >
SqFunction<RT> Get(TypeWrapper<SqFunction<RT>>,HSQUIRRELVM v,SQInteger index)
{
	SqTypeWrapper x4(stack_get(v, index));	
	if (x4.sqobj._type == OT_CLOSURE)
	{
		return SqFunction<RT>(v,x4.sqobj._unVal.pClosure->_function->_name._unVal.pString->_val); 

	}else if (x4.sqobj._type == OT_STRING)
	{
		return 	SqFunction<RT> (v,x4.sqobj._unVal.pString->_val); 
	}else
	{
		return SqFunction<RT>(v,NULL);
	}
}
template <>
SqFunction<void> Get(TypeWrapper<SqFunction<void>>,HSQUIRRELVM v,SQInteger index)
{
	SqTypeWrapper x4(stack_get(v, index));	
	if (x4.sqobj._type == OT_CLOSURE)
	{
		return SqFunction<void>(v,x4.sqobj._unVal.pClosure->_function->_name._unVal.pString->_val); 

	}else if (x4.sqobj._type == OT_STRING)
	{
		return 	SqFunction<void> (v,x4.sqobj._unVal.pString->_val); 
	}else
	{
		return SqFunction<void>(v,NULL);
	}

}
#define REGGLOBALFUNC(NAME) RegisterGlobal(v,##NAME,_SC(#NAME));
#define SQ_REGGLOBALFUNC(NAME) register_global_func(v,sq_##NAME,_SC(#NAME));

//typedef struct _HPDF_Doc_Rec  *HPDF_Doc;
template <>
HPDF_LineJoin Get(TypeWrapper<HPDF_LineJoin>,HSQUIRRELVM v,SQInteger index)
{


	return (HPDF_LineJoin)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_UINT16 Get(TypeWrapper<HPDF_UINT16>,HSQUIRRELVM v,SQInteger index)
{


	return (HPDF_UINT16)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_EncryptMode Get(TypeWrapper<HPDF_EncryptMode>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_EncryptMode)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_TransitionStyle Get(TypeWrapper<HPDF_TransitionStyle>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_TransitionStyle)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_PageNumStyle Get(TypeWrapper<HPDF_PageNumStyle>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_PageNumStyle)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_PageDirection Get(TypeWrapper<HPDF_PageDirection>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_PageDirection)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_InfoType Get(TypeWrapper<HPDF_InfoType>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_InfoType)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_PageSizes Get(TypeWrapper<HPDF_PageSizes>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_PageSizes)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_MMgr Get(TypeWrapper<HPDF_MMgr>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_MMgr)static_cast<void*>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_Stream Get(TypeWrapper<HPDF_Stream>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_Stream)static_cast<void*>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_Xref Get(TypeWrapper<HPDF_Xref>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_Xref)static_cast<void*>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_Page Get(TypeWrapper<HPDF_Page>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_Page)static_cast<void*>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_Destination Get(TypeWrapper<HPDF_Destination>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_Destination)static_cast<void*>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_ColorSpace Get(TypeWrapper<HPDF_ColorSpace>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_ColorSpace)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_LineAnnotCapPosition Get(TypeWrapper<HPDF_LineAnnotCapPosition>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_LineAnnotCapPosition)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_LineAnnotEndingStyle Get(TypeWrapper<HPDF_LineAnnotEndingStyle>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_LineAnnotEndingStyle)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_AnnotIntent Get(TypeWrapper<HPDF_AnnotIntent>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_AnnotIntent)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_AnnotIcon Get(TypeWrapper<HPDF_AnnotIcon>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_AnnotIcon)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_BSSubtype Get(TypeWrapper<HPDF_BSSubtype>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_BSSubtype)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_AnnotHighlightMode Get(TypeWrapper<HPDF_AnnotHighlightMode>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_AnnotHighlightMode)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}

template <>
HPDF_StampAnnotName Get(TypeWrapper<HPDF_StampAnnotName>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_StampAnnotName)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}

template <>
HPDF_AnnotType Get(TypeWrapper<HPDF_AnnotType>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_AnnotType)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}

template <>
HPDF_LineCap Get(TypeWrapper<HPDF_LineCap>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_LineCap)static_cast<int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_Encoder Get(TypeWrapper<HPDF_Encoder>,HSQUIRRELVM v,SQInteger index)
{
	SqTypeWrapper x4(stack_get(v, index));

	return (HPDF_Encoder)static_cast<void*>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_Doc Get(TypeWrapper<HPDF_Doc>,HSQUIRRELVM v,SQInteger index)
{
	SqTypeWrapper x4(stack_get(v, index));

	return (HPDF_Doc)static_cast<void*>(SqTypeWrapper(stack_get(v, index)));
}
//typedef struct _HPDF_Dict_Rec  *HPDF_Dict;
// template <>
// HPDF_TransMatrix Get(TypeWrapper<HPDF_TransMatrix>,HSQUIRRELVM v,SQInteger index)
// {
// 	return (HPDF_TransMatrix)static_cast<void*>(SqTypeWrapper(stack_get(v, index)));
// }
template <>
HPDF_STATUS Get(TypeWrapper<HPDF_STATUS>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_STATUS)static_cast<unsigned int>(SqTypeWrapper(stack_get(v, index)));
}

template <>
HPDF_PageLayout Get(TypeWrapper<HPDF_PageLayout>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_PageLayout)static_cast<unsigned int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_PageMode Get(TypeWrapper<HPDF_PageMode>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_PageMode)static_cast<unsigned int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_BlendMode Get(TypeWrapper<HPDF_BlendMode>,HSQUIRRELVM v,SQInteger index)
{
	return (HPDF_BlendMode)static_cast<unsigned int>(SqTypeWrapper(stack_get(v, index)));
}
template <>
HPDF_Error_Handler Get(TypeWrapper<HPDF_Error_Handler>,HSQUIRRELVM v,SQInteger index)
{
	HaruErrofHandler = Get(TypeWrapper<SqFunction<void>>(),v,index); //static_cast<SqFunction<void>>(SqTypeWrapper(stack_get(v, index)));
	return error_handler;
}
template <>
HPDF_Alloc_Func Get(TypeWrapper<HPDF_Alloc_Func>,HSQUIRRELVM v,SQInteger index)
{
	Haru_Alloc_Func = Get(TypeWrapper<SqFunction<void*>>(),v,index); //static_cast<SqFunction<void>>(SqTypeWrapper(stack_get(v, index)));
	return g_HPDF_Alloc_Func;
}
template <>
HPDF_Free_Func Get(TypeWrapper<HPDF_Free_Func>,HSQUIRRELVM v,SQInteger index)
{
	Haru_Free_Func = Get(TypeWrapper<SqFunction<void>>(),v,index); //static_cast<SqFunction<void>>(SqTypeWrapper(stack_get(v, index)));
	return g_HPDF_Free_Func;
}
// template <>
// HPDF_BlendMode Get(TypeWrapper<HPDF_BlendMode>,HSQUIRRELVM v,SQInteger index)
// {
// 	return (HPDF_BlendMode)static_cast<unsigned int>(SqTypeWrapper(stack_get(v, index)));
// }

template <>
void Push(HSQUIRRELVM v,HPDF_STATUS p1)
{
v->Push((int)p1);
}



HPDF_STATUS HPDF_Page_SetDash_SqCap  (HPDF_Page           page,
					SQObjectPtr  dash_ptn,
					HPDF_UINT           num_param,
					HPDF_UINT           phase)
{
HPDF_UINT16  *dash_ptn2 = NULL;
if (dash_ptn._type == OT_ARRAY)
	{
		dash_ptn2 = new HPDF_UINT16 [dash_ptn._unVal.pArray->_values.size()];
		for ( int i = 0;i<dash_ptn._unVal.pArray->_values.size();i++)
		{
			dash_ptn2[i] = dash_ptn._unVal.pArray->_values[i]._unVal.nInteger;
		}
	}

	HPDF_STATUS  rtn = HPDF_Page_SetDash(page,dash_ptn2,num_param,phase);
	if (dash_ptn2!= NULL)
	{
		delete []dash_ptn2;
	}
	return rtn;
}
// HPDF_Doc HPDF_New_SQCap(SqFunction<void> errorhandler,void* userdata)
// {
// 	HaruErrofHandler = errorhandler;
// 
// 	return HPDF_New (error_handler,userdata);
// }
HPDF_Doc HPDF_NewEx_SQCap(SqFunction<void> errorhandler,SqFunction<void*> Alloc_Func, SqFunction<void> Free_Func, HPDF_UINT size,void* userdata)
{
	HaruErrofHandler = errorhandler;
	Haru_Alloc_Func = Alloc_Func;
	Haru_Free_Func = Free_Func;

	return HPDF_NewEx (error_handler, g_HPDF_Alloc_Func, g_HPDF_Free_Func, size, userdata);
}


SQInteger sq_HPDF_NewEx(HSQUIRRELVM v)
{
	HPDF_Doc  pdf;
	pdf = HPDF_NewEx (error_handler, NULL, NULL, 0, NULL);
//	sq_pushinteger(v,pdf); //push the number of arguments as return value
	sq_pushuserpointer(v,pdf);
	printf("pdf:%08x\n",pdf);	
	return 1;
}
// HPDF_STATUS sq_HPDF_SaveToFilex(SQUserPointer pdf,const char  *file_name)
// {
// 	return HPDF_SaveToFile((HPDF_Doc)pdf,file_name);
// }

SQInteger sq_HPDF_SaveToFile(HSQUIRRELVM v)
{
// 	HPDF_Doc  pdf;
// 	pdf = HPDF_New (error_handler, NULL);
	SQUserPointer pdf;
	sq_getuserpointer(v,2,&pdf);
	const SQChar* filename;
	sq_getstring(v,3,&filename);
	HPDF_STATUS rtn = HPDF_SaveToFile((HPDF_Doc)pdf,filename);

//	sq_pushinteger(v,pdf); //push the number of arguments as return value
	sq_pushinteger(v,rtn);

//	sq_pushuserpointer(v,pdf);
	return 1;
}

SQInteger sq_HPDF_GetFont(HSQUIRRELVM v)
{
	printf("sq_HPDF_GetFont\n");
	SQUserPointer pdf;
	sq_getuserpointer(v,2,&pdf);
	printf("pdf:%08x\n",pdf);
	const SQChar* font_name;
	sq_getstring(v,3,&font_name);
	const SQChar* encoding_name;
	if (sq_getstring(v,4,&encoding_name)==SQ_ERROR)
	{
		encoding_name = NULL;
	}
	;
	printf("font:%s\t",font_name);

	SqTypeWrapper x4(stack_get(v, 4));
	
	if (x4.sqobj._type == OT_NULL)
	{
		encoding_name = NULL;
	}




	HPDF_Font rtn = HPDF_GetFont((HPDF_Doc)pdf,font_name,encoding_name);
//	sq_pushinteger(v,pdf); //push the number of arguments as return value


	sq_pushuserpointer(v,rtn);
	return 1;
}


//////////////////////////////////////////////////////////////////////////

_DECL_CLASS(HPDF_RGBColor);

_IMPL_NATIVE_CONSTRUCTION(HPDF_RGBColor,HPDF_RGBColor);

_MEMBER_FUNCTION_IMPL(HPDF_RGBColor,constructor)
{
	HPDF_RGBColor temp;
	HPDF_RGBColor *newv = NULL;
	StackHandler sa(v);
	int nparams = sa.GetParamCount();
	switch(nparams) {
	case 1:
		temp.r = 0; 
		temp.g = 0;
		temp.b = 0;
		break;
	case 2:
		if(sa.GetType(2) == OT_INSTANCE) {
			_CHECK_INST_PARAM(vec,2,HPDF_RGBColor,HPDF_RGBColor);
			if(vec)	temp = *vec;
			else return sa.ThrowError(_T("HPDF_RGBColor() invalid instance type"));
		}
		break;
	case 4:
		temp.r = sa.GetFloat(2);
		temp.g = sa.GetFloat(3);
		temp.b = sa.GetFloat(4);
		break;
	default:
		return -1/*sa.ThrowError(_T("Vector3() wrong parameters"))*/;
	}
	newv = new HPDF_RGBColor(temp);
	return construct_HPDF_RGBColor(v,newv);
}
_MEMBER_FUNCTION_IMPL(HPDF_RGBColor,_get)
{
	StackHandler sa(v);
	_CHECK_SELF(HPDF_RGBColor,HPDF_RGBColor);
	const SQChar *s = sa.GetString(2);
	if(s && (s[1] != 0))
		return SQ_ERROR;
	int index = s && (s[1] == 0)?s[0]:sa.GetInt(2);
	switch(index) {
		case 0: case 'x': case 'r': return sa.Return(self->r); break;
		case 1: case 'y': case 'g':	return sa.Return(self->g); break;
		case 2: case 'z': case 'b': return sa.Return(self->b); break;
	}
	return SQ_ERROR;
}


_MEMBER_FUNCTION_IMPL(HPDF_RGBColor,_set)
{
	StackHandler sa(v);
	_CHECK_SELF(HPDF_RGBColor,HPDF_RGBColor);
	const SQChar *s = sa.GetString(2);
	int index = s?s[0]:sa.GetInt(2);
	switch(index) {
	case 0: case 'x': case 'r':
		return sa.Return(self->r = sa.GetFloat(3));
		break;
	case 1: case 'y': case 'g':
		return sa.Return(self->g = sa.GetFloat(3));
		break;
	case 2: case 'z': case 'b':
		return sa.Return(self->b = sa.GetFloat(3));
		break;
	}

	return SQ_ERROR;
}


_BEGIN_CLASS(HPDF_RGBColor)
_MEMBER_FUNCTION(HPDF_RGBColor,constructor,-1,(".n|xnn"))
_MEMBER_FUNCTION(HPDF_RGBColor,_set,3,_T("xs|n"))
_MEMBER_FUNCTION(HPDF_RGBColor,_get,2,_T("xs|n"))
_END_CLASS(HPDF_RGBColor)
void functest(HPDF_RGBColor tstclr)
{
	printf("\nfunctest::R:%.3f,G:%.3f,B:%.3f\n",tstclr.r,tstclr.g,tstclr.b);

}
template <>
HPDF_RGBColor Get(TypeWrapper<HPDF_RGBColor>,HSQUIRRELVM v,SQInteger index)
{
	SqTypeWrapper x4(stack_get(v, index));
	if (x4.sqobj._type== OT_INSTANCE)
	{
		HPDF_RGBColor* pd3dvec = (HPDF_RGBColor*)(x4.sqobj._unVal.pInstance->_userpointer);
		return *pd3dvec;
		v->Push(x4.sqobj);

	}else{
		return HPDF_RGBColor();
	}
}
template <>
void Push(HSQUIRRELVM v,HPDF_RGBColor p1)
{
	//sq_pushuserpointer(v,&p1);
	//v->Push(new_H_RGBColor(v,p1));
	//new_H_RGBColor(v,p1);
	//StackHandler sa(v);
	//construct_H_RGBColor(v,new HPDF_RGBColor(p1));
	v->Push(new_HPDF_RGBColor(v,p1));
//	sq_pop(v,1);

}
HPDF_RGBColor functest2()
{
	HPDF_RGBColor tstclr;
	tstclr.r = 0.5;
	tstclr.g = 2.5;
	tstclr.b = 4.5;

	printf("\nfunctest2::R:%.3f,G:%.3f,B:%.3f\n",tstclr.r,tstclr.g,tstclr.b);
	return tstclr;

}

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//HPDF_Box
_DECL_CLASS(HPDF_Box);

_IMPL_NATIVE_CONSTRUCTION(HPDF_Box,HPDF_Box);

_MEMBER_FUNCTION_IMPL(HPDF_Box,constructor)
{
	HPDF_Box temp;
	HPDF_Box *newv = NULL;
	StackHandler sa(v);
	int nparams = sa.GetParamCount();
	switch(nparams) {
	case 1:
		temp.left = 0; 
		temp.bottom = 0;
		temp.right = 0;
		temp.top = 0;
		break;
	case 2:
		if(sa.GetType(2) == OT_INSTANCE) {
			_CHECK_INST_PARAM(vec,2,HPDF_Box,HPDF_Box);
			if(vec)	temp = *vec;
			else return sa.ThrowError(_T("HPDF_CMYKColor() invalid instance type"));
		}
		break;
	case 5:
		temp.left = sa.GetFloat(2);
		temp.bottom = sa.GetFloat(3);
		temp.right = sa.GetFloat(4);
		temp.top = sa.GetFloat(5);

		break;
	default:
		return -1/*sa.ThrowError(_T("Vector3() wrong parameters"))*/;
	}
	newv = new HPDF_Box(temp);
	return construct_HPDF_Box(v,newv);
}
_MEMBER_FUNCTION_IMPL(HPDF_Box,_get)
{
//	printf("%s","HPDF_Box::_get\n");
	StackHandler sa(v);
	_CHECK_SELF(HPDF_Box,HPDF_Box);
// printf("%s","HPDF_Box::_get\n");
	const SQChar *s = sa.GetString(2);
// 	if(s && (s[1] != 0))
// 	{
// 		printf("%s","HPDF_Box::_get\n");
// 		return SQ_ERROR;
// 
// 	}
//	printf("HPDF_Box::_get(%s)\n",s);
	int index = s && (s[1] == 0)?s[0]:sa.GetInt(2);
	if (s[0]>'0'&&s[0]<'9')
	{
		index =sa.GetInt(2); 
	}else{
		index = s[0];
	}
//	printf("index:%d",index);
	switch(index) {
		case 0: case 'l':  return sa.Return(self->left); break;
		case 1: case 'b':  return sa.Return(self->bottom); break;
		case 2: case 'r':  return sa.Return(self->right); break;
		case 3: case 't':  return sa.Return(self->top); break;
	}
	return SQ_ERROR;
}


_MEMBER_FUNCTION_IMPL(HPDF_Box,_set)
{
	StackHandler sa(v);
	_CHECK_SELF(HPDF_Box,HPDF_Box);
	const SQChar *s = sa.GetString(2);
	int index = s?s[0]:sa.GetInt(2);
	switch(index) {
	case 0: case 'l': 
		return sa.Return(self->left = sa.GetFloat(3));
		break;
	case 1: case 'b':
		return sa.Return(self->bottom = sa.GetFloat(3));
		break;
	case 2: case 'r':
		return sa.Return(self->right = sa.GetFloat(3));
		break;
	case 3: case 't':
		return sa.Return(self->top = sa.GetFloat(3));
		break;
	}

	return SQ_ERROR;
}


_BEGIN_CLASS(HPDF_Box)
_MEMBER_FUNCTION(HPDF_Box,constructor,-1,(".n|xnn"))
_MEMBER_FUNCTION(HPDF_Box,_set,3,_T("xs|n"))
_MEMBER_FUNCTION(HPDF_Box,_get,2,_T("xs|n"))
_END_CLASS(HPDF_Box)

template <>
HPDF_Box Get(TypeWrapper<HPDF_Box>,HSQUIRRELVM v,SQInteger index)
{
	SqTypeWrapper x4(stack_get(v, index));
	if (x4.sqobj._type== OT_INSTANCE)
	{
		HPDF_Box* pd3dvec = (HPDF_Box*)(x4.sqobj._unVal.pInstance->_userpointer);
		return *pd3dvec;
		v->Push(x4.sqobj);

	}else{
		return HPDF_Box();
	}
}
template <>
void Push(HSQUIRRELVM v,HPDF_Box p1)
{
	v->Push(new_HPDF_Box(v,p1));
}

//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
//HPDF_CMYKColor
_DECL_CLASS(HPDF_CMYKColor);

_IMPL_NATIVE_CONSTRUCTION(HPDF_CMYKColor,HPDF_CMYKColor);

_MEMBER_FUNCTION_IMPL(HPDF_CMYKColor,constructor)
{
	HPDF_CMYKColor temp;
	HPDF_CMYKColor *newv = NULL;
	StackHandler sa(v);
	int nparams = sa.GetParamCount();
	switch(nparams) {
	case 1:
		temp.c = 0; 
		temp.m = 0;
		temp.y = 0;
		temp.k = 0;
		break;
	case 2:
		if(sa.GetType(2) == OT_INSTANCE) {
			_CHECK_INST_PARAM(vec,2,HPDF_CMYKColor,HPDF_CMYKColor);
			if(vec)	temp = *vec;
			else return sa.ThrowError(_T("HPDF_CMYKColor() invalid instance type"));
		}
		break;
	case 5:
		temp.c = sa.GetFloat(2);
		temp.m = sa.GetFloat(3);
		temp.y = sa.GetFloat(4);
		temp.k = sa.GetFloat(4);

		break;
	default:
		return -1/*sa.ThrowError(_T("Vector3() wrong parameters"))*/;
	}
	newv = new HPDF_CMYKColor(temp);
	return construct_HPDF_CMYKColor(v,newv);
}
_MEMBER_FUNCTION_IMPL(HPDF_CMYKColor,_get)
{
	StackHandler sa(v);
	_CHECK_SELF(HPDF_CMYKColor,HPDF_CMYKColor);
	const SQChar *s = sa.GetString(2);
	if(s && (s[1] != 0))
		return SQ_ERROR;
	int index = s && (s[1] == 0)?s[0]:sa.GetInt(2);
	switch(index) {
		case 0: case 'c': case 'r': return sa.Return(self->c); break;
		case 1: case 'm': case 'g':	return sa.Return(self->m); break;
		case 2: case 'y': case 'b': return sa.Return(self->y); break;
		case 3: case 'k': case 'K': return sa.Return(self->k); break;
	}
	return SQ_ERROR;
}


_MEMBER_FUNCTION_IMPL(HPDF_CMYKColor,_set)
{
	StackHandler sa(v);
	_CHECK_SELF(HPDF_CMYKColor,HPDF_CMYKColor);
	const SQChar *s = sa.GetString(2);
	int index = s?s[0]:sa.GetInt(2);
	switch(index) {
	case 0: case 'c': case 'r':
		return sa.Return(self->c = sa.GetFloat(3));
		break;
	case 1: case 'm': case 'g':
		return sa.Return(self->m = sa.GetFloat(3));
		break;
	case 2: case 'y': case 'b':
		return sa.Return(self->y = sa.GetFloat(3));
		break;
	case 3: case 'k': case 'K':
		return sa.Return(self->k = sa.GetFloat(3));
		break;
	}

	return SQ_ERROR;
}


_BEGIN_CLASS(HPDF_CMYKColor)
_MEMBER_FUNCTION(HPDF_CMYKColor,constructor,-1,(".n|xnn"))
_MEMBER_FUNCTION(HPDF_CMYKColor,_set,3,_T("xs|n"))
_MEMBER_FUNCTION(HPDF_CMYKColor,_get,2,_T("xs|n"))
_END_CLASS(HPDF_CMYKColor)

template <>
HPDF_CMYKColor Get(TypeWrapper<HPDF_CMYKColor>,HSQUIRRELVM v,SQInteger index)
{
	SqTypeWrapper x4(stack_get(v, index));
	if (x4.sqobj._type== OT_INSTANCE)
	{
		HPDF_CMYKColor* pd3dvec = (HPDF_CMYKColor*)(x4.sqobj._unVal.pInstance->_userpointer);
		return *pd3dvec;
		v->Push(x4.sqobj);

	}else{
		return HPDF_CMYKColor();
	}
}
template <>
void Push(HSQUIRRELVM v,HPDF_CMYKColor p1)
{
	v->Push(new_HPDF_CMYKColor(v,p1));
}

//////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////
//HPDF_Point
_DECL_CLASS(HPDF_Point);

_IMPL_NATIVE_CONSTRUCTION(HPDF_Point,HPDF_Point);

_MEMBER_FUNCTION_IMPL(HPDF_Point,constructor)
{
	HPDF_Point temp;
	HPDF_Point *newv = NULL;
	StackHandler sa(v);
	int nparams = sa.GetParamCount();
	switch(nparams) {
	case 1:
		temp.x = 0; 
		temp.y = 0;
		break;
	case 2:
		if(sa.GetType(2) == OT_INSTANCE) {
			_CHECK_INST_PARAM(pnt,2,HPDF_Point,HPDF_Point);
			if(pnt)	temp = *pnt;
			else return sa.ThrowError(_T("HPDF_Point() invalid instance type"));
		}
		break;
	case 3:
		temp.x = sa.GetFloat(2);
		temp.y = sa.GetFloat(3);


		break;
	default:
		return -1/*sa.ThrowError(_T("Vector3() wrong parameters"))*/;
	}
	newv = new HPDF_Point(temp);
	return construct_HPDF_Point(v,newv);
}
_MEMBER_FUNCTION_IMPL(HPDF_Point,_get)
{
	StackHandler sa(v);
	_CHECK_SELF(HPDF_Point,HPDF_Point);
	const SQChar *s = sa.GetString(2);
	if(s && (s[1] != 0))
		return SQ_ERROR;
	int index = s && (s[1] == 0)?s[0]:sa.GetInt(2);
	switch(index) {
		case 0: case 'x': case 'r': return sa.Return(self->x); break;
		case 1: case 'y': case 'b': return sa.Return(self->y); break;
	}
	return SQ_ERROR;
}


_MEMBER_FUNCTION_IMPL(HPDF_Point,_set)
{
	StackHandler sa(v);
	_CHECK_SELF(HPDF_Point,HPDF_Point);
	const SQChar *s = sa.GetString(2);
	int index = s?s[0]:sa.GetInt(2);
	switch(index) {
	case 0: case 'x': case 'r':
		return sa.Return(self->x = sa.GetFloat(3));
		break;
	case 1: case 'y': case 'g':
		return sa.Return(self->y = sa.GetFloat(3));
		break;

	}

	return SQ_ERROR;
}


_BEGIN_CLASS(HPDF_Point)
_MEMBER_FUNCTION(HPDF_Point,constructor,-1,(".n|xnn"))
_MEMBER_FUNCTION(HPDF_Point,_set,3,_T("xs|n"))
_MEMBER_FUNCTION(HPDF_Point,_get,2,_T("xs|n"))
_END_CLASS(HPDF_Point)

template <>
HPDF_Point Get(TypeWrapper<HPDF_Point>,HSQUIRRELVM v,SQInteger index)
{
	SqTypeWrapper x4(stack_get(v, index));
	if (x4.sqobj._type== OT_INSTANCE)
	{
		HPDF_Point* pd3dvec = (HPDF_Point*)(x4.sqobj._unVal.pInstance->_userpointer);
		return *pd3dvec;

	}else{
		return HPDF_Point();
	}
}
template <>
HPDF_Point*Get(TypeWrapper<HPDF_Point*>,HSQUIRRELVM v,SQInteger index)
{
	SqTypeWrapper x4(stack_get(v, index));
	if (x4.sqobj._type== OT_INSTANCE)
	{
		HPDF_Point* pd3dvec = (HPDF_Point*)(x4.sqobj._unVal.pInstance->_userpointer);
		return pd3dvec;

	}else{
		return NULL;
	}
}
template <>
void Push(HSQUIRRELVM v,HPDF_Point p1)
{
	v->Push(new_HPDF_Point(v,p1));
}
#ifdef TESTCODE
void functest3(HPDF_Point* pnt)
{
	pnt->x = 0;
	pnt->y = 0;
}
#endif // TESTCODE

//////////////////////////////////////////////////////////////////////////




void HaruBind(HSQUIRRELVM v)
{
#ifdef TESTCODE
		register_global_func(v,print_args,_SC("PrintArg"));
		_INIT_CLASS(v,Vector3);

		REGGLOBALFUNC(functest)
		REGGLOBALFUNC(functest2)
		REGGLOBALFUNC(functest3)
#endif // TESTCODE
		_INIT_CLASS(v,HPDF_RGBColor);
		_INIT_CLASS(v,HPDF_CMYKColor);
		_INIT_CLASS(v,HPDF_Point);
		_INIT_CLASS(v,HPDF_Box);
//  RegisterGlobal(v,HPDF_NewEx_SQCap,_SC("HPDF_NewEx"));
// 	RegisterGlobal(v,HPDF_New_SQCap,_SC("HPDF_New"));
//	RegisterGlobal(v,HPDF_Page_SetDash_SqCap,_SC("HPDF_Page_SetDash"));

#include "hpdfbind.c"



}


#endif

